/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProfileSelection2.java
 *
 * Created on Oct 4, 2010, 8:56:32 PM
 */

package Java;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import java.text.SimpleDateFormat;
import java.util.Date;




/**
 *
 * @author shyan
 */
public class EMPLOYEE_MENU extends javax.swing.JFrame {

    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String userID = "HR";
    private String userPW = "hr";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;

    public static String ID="";
    public static String Name="";
    public static String Position="";

    /** Creates new form ProfileSelection2 */
    public EMPLOYEE_MENU()
    {
        super("MAIN MENU");
        initComponents();
        setLocationRelativeTo(null);
        setTimer();
        update();
        idField.setText(ID);
        nameField.setText(Name);
        positionField.setText(Position);

        
    }

    EMPLOYEE_MENU(String id,String name,String position)
    {
        initComponents();
        setLocationRelativeTo(null);
        setTimer();
        update();
        System.out.println("Current user :"+id);
        ID=id;
        Name=name;
        Position=position;
         
        idField.setText(ID);
        nameField.setText(Name);
        positionField.setText(Position);
            
        if(Position.equals("Staff"))
        {
            EmployeeProfile.setEnabled(false);
            //SalesProfile.setEnabled(false);
            MenuProfile.setEnabled(false);
        }
        else if (Position.equals("Supervisor"))
        {
            EmployeeProfile.setEnabled(false);
            //SalesProfile.setEnabled(false);
        }
        else
        {
            System.out.println("FULLY ACCESS !!!");
        }
    }
    
 //***************************** DATEBASE CONNECTION PART ******************************
    private void connectDatabase()
    {
        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            con=DriverManager.getConnection(url, userID, userPW);
        }

        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (SQLException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //***************************** DATEBASE EXECUTE PART ******************************
    private ResultSet getResultSet(String doubt)
    {
        connectDatabase();
        ResultSet outcome = null;
        try
        {
            stmt = con.createStatement();
            outcome = stmt.executeQuery(doubt);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }
        return outcome;
    }
    
    //***************************** DATEBASE UPDATE PART ******************************
    private void updateRow(String edit)
    {
        connectDatabase();
        try
        {
            stmt = con.createStatement();
            stmt.executeUpdate(edit);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

   //*************************** CURRENT DATE & TIME PART *****************************
    public int time = 0;
    private Timer timer;
    public void setTimer()
    {
        //Timer timer;
        int messageTimeout = 1000;
        timer = new Timer
                (
                  messageTimeout, new ActionListener()
                  {
                    public void actionPerformed(ActionEvent e){
                    update();}
                  }
                 );
        timer.restart();
    }
    String dateString;//Global variable
    public void update()
    {
        Date currentTime = new Date();
        //int time=0;
        time++;

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
        SimpleDateFormat pc1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat pc=new SimpleDateFormat("HHmm"); //24Hour time format
        dateString = formatter.format(currentTime);
        zzz.setText(dateString);

        int omg =Integer.parseInt(pc1.format(currentTime)); // CURRENT DATE
        int haiz=Integer.parseInt(pc.format(currentTime));  // CURRENT TIME

        System.out.println("OMG ->"+omg);
        System.out.println("HAIZ->"+haiz);
        
        if(time>2)
        {

            try
            {
                int hope = 0;
                int sigh = 0;
                String year="";
                String month="";
                String day="";
                String date="";
                String Rnum="";
                             
                rset=getResultSet("SELECT * FROM RESERVATION");
                while(rset.next())
                {
                    Rnum=rset.getString("RESERVATION_NUM");
                    sigh = Integer.parseInt(rset.getString("RESERVATION_TIME"));

                    date = rset.getString("RESERVATION_DATE");
                    System.out.println("shit-> "+sigh+"  "+date);
                    year=date.substring(0, 4);
                    month=date.substring(5, 7);
                    day=date.substring(8, 10);
                    System.out.println(year+month+day);
                    hope=Integer.parseInt(year+month+day);
                    System.out.println("HOPE -> "+hope);

                    if(omg==hope && haiz < sigh )
                    {
                        AlertField.setText("(Today got reservation !)");
                    }

                    if(omg > hope || (omg == hope && haiz > sigh+1))
                    {
                        
                        String remove="DELETE FROM RESERVATION WHERE RESERVATION_NUM="+Rnum;
                        updateRow(remove);
                        AlertField.setText("");
                        System.out.println("DIE DIE DIE DIE DIE .... =.=");
                    }
                    else
                    {
                        System.out.println("Nothing");
                    }
                }
                con.close();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }


            time=0;   
        }
        //String a=pc.format(currentTime);
        //System.out.println(a);
    }
//----------------------------END OF CURRENT DATE & TIME PART--------------------------



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        title = new javax.swing.JLabel();
        Logout = new javax.swing.JLabel();
        idlabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        positionLabel = new javax.swing.JLabel();
        idField = new javax.swing.JLabel();
        nameField = new javax.swing.JLabel();
        positionField = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        EmployeeProfile = new javax.swing.JButton();
        MenuProfile = new javax.swing.JButton();
        Cashier = new javax.swing.JButton();
        Reservation = new javax.swing.JButton();
        AlertField = new javax.swing.JLabel();
        zzz = new javax.swing.JLabel();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 300));
        setResizable(false);

        jPanel1.setMinimumSize(new java.awt.Dimension(400, 300));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanel1.setLayout(null);

        jSeparator1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 65, 500, 4);

        title.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 24));
        title.setText("PROFILE SELECTION");
        jPanel1.add(title);
        title.setBounds(40, 20, 290, 33);

        Logout.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 14));
        Logout.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Logout.setText("Logout");
        Logout.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        Logout.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LogoutMouseClicked(evt);
            }
        });
        jPanel1.add(Logout);
        Logout.setBounds(400, 10, 80, 40);

        idlabel.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 12));
        idlabel.setText("ID:");
        jPanel1.add(idlabel);
        idlabel.setBounds(280, 140, 30, 14);

        nameLabel.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 12));
        nameLabel.setText("Name:");
        jPanel1.add(nameLabel);
        nameLabel.setBounds(280, 180, 39, 14);

        positionLabel.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 12));
        positionLabel.setText("Position:");
        jPanel1.add(positionLabel);
        positionLabel.setBounds(280, 220, 70, 20);

        idField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13));
        idField.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel1.add(idField);
        idField.setBounds(340, 140, 90, 20);

        nameField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13));
        nameField.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel1.add(nameField);
        nameField.setBounds(340, 180, 140, 20);

        positionField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13));
        positionField.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel1.add(positionField);
        positionField.setBounds(340, 220, 90, 20);

        jLabel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Current user", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif Condensed", 3, 14))); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(270, 100, 220, 160);

        EmployeeProfile.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        EmployeeProfile.setText("Employee profile");
        EmployeeProfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EmployeeProfileActionPerformed(evt);
            }
        });
        jPanel1.add(EmployeeProfile);
        EmployeeProfile.setBounds(40, 100, 170, 40);

        MenuProfile.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        MenuProfile.setText("Menu profile");
        MenuProfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuProfileActionPerformed(evt);
            }
        });
        jPanel1.add(MenuProfile);
        MenuProfile.setBounds(40, 160, 170, 40);

        Cashier.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        Cashier.setText("Cashier");
        Cashier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CashierActionPerformed(evt);
            }
        });
        jPanel1.add(Cashier);
        Cashier.setBounds(40, 220, 170, 40);

        Reservation.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        Reservation.setText("Reservation");
        Reservation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReservationActionPerformed(evt);
            }
        });
        jPanel1.add(Reservation);
        Reservation.setBounds(40, 280, 170, 40);

        AlertField.setFont(new java.awt.Font("DejaVu Serif Condensed", 2, 14)); // NOI18N
        AlertField.setForeground(new java.awt.Color(254, 13, 22));
        jPanel1.add(AlertField);
        AlertField.setBounds(220, 280, 160, 40);

        zzz.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13));
        jPanel1.add(zzz);
        zzz.setBounds(360, 70, 130, 20);

        background.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        background.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        jPanel1.add(background);
        background.setBounds(0, 0, 500, 400);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LogoutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LogoutMouseClicked
        timer.stop();
        this.setVisible(false);
        //new EMPLOYEE_LOGIN().setVisible(true);
        new FIRST_SCREEN().setVisible(true);
    }//GEN-LAST:event_LogoutMouseClicked

    private void EmployeeProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EmployeeProfileActionPerformed
        timer.stop();
        this.setVisible(false);
        new EMPLOYEE_PROFILE().setVisible(true);
    }//GEN-LAST:event_EmployeeProfileActionPerformed

    private void MenuProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuProfileActionPerformed
        timer.stop();
        this.setVisible(false);
        try
        {
            new MENU_EDIT().setVisible(true);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EMPLOYEE_MENU.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MenuProfileActionPerformed

    private void CashierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CashierActionPerformed
        timer.stop();
        this.setVisible(false);
        new RECEIPT().setVisible(true);
    }//GEN-LAST:event_CashierActionPerformed

    private void ReservationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReservationActionPerformed
        timer.stop();
        this.setVisible(false);
        new CUS_RESERVATION_EDIT().setVisible(true);
    }//GEN-LAST:event_ReservationActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EMPLOYEE_MENU().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AlertField;
    private javax.swing.JButton Cashier;
    private javax.swing.JButton EmployeeProfile;
    private javax.swing.JLabel Logout;
    private javax.swing.JButton MenuProfile;
    private javax.swing.JButton Reservation;
    private javax.swing.JLabel background;
    private javax.swing.JLabel idField;
    private javax.swing.JLabel idlabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel positionField;
    private javax.swing.JLabel positionLabel;
    private javax.swing.JLabel title;
    private javax.swing.JLabel zzz;
    // End of variables declaration//GEN-END:variables

}
